﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crawler_v1.entity;

namespace Crawler_v1.service
{
    class ArticleService
    {

        public void list(PageInfo pageInfo)
        {
            //where
            string where = " where 1=1";
            foreach (KeyValuePair<string, string> kvp in pageInfo.Conditions)
            {
                if (kvp.Key == "name")
                {
                    //搜索
                    where += " and (title like '%" + kvp.Value + "%' or content like '%" + kvp.Value + "%' )";
                }
                else if (kvp.Key == "ispost")
                {
                    where += " and ispost = " + kvp.Value;
                }
                else if (kvp.Key == "column")
                {
                    where += " and column = '" + kvp.Value + "'";
                }
                //Console.WriteLine("姓名：{0},电影：{1}", kvp.Key, kvp.Value);
            }

            //数量
            string sql = "select count(*) as dd from article " + where;
            object count = SQLiteHelper.GetSingle(sql);

            int total_item = Convert.ToInt32(count.ToString());
            int total_page = ((total_item + pageInfo.PageSize - 1) / pageInfo.PageSize);

            pageInfo.TotalItem = total_item;
            pageInfo.TotalPage = total_page;



            int form = (pageInfo.CurPage - 1) * pageInfo.PageSize;

            //sql = "select * from article  " + where + " order by id desc limit " + form + "," + pageInfo.PageSize;
            sql = @"select 
id as my_id,
title as my_title,
content as my_content,
time as my_time,
column as my_column,
parent as my_parent,
url as my_url,
ispost as my_ispost,
create_time as my_create_time
from article " + where + " order by id desc limit " + form + "," + pageInfo.PageSize;

            pageInfo.DataTable = SQLiteHelper.Query(sql).Tables[0];


        }

        public PageInfo findAllNotPost()
        {
            PageInfo pageInfo = new PageInfo();
            pageInfo.CurPage = 1;
            pageInfo.PageSize = 99999;
            pageInfo.Conditions = new Dictionary<string, string>();
            pageInfo.Conditions.Add("ispost", "0");
            list(pageInfo);
            return pageInfo;
        }

        /// <summary>
        /// 设置文章的状态已经提交了
        /// </summary>
        public void setPost()
        {

        }
    }
}
